package zcj.stu;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;

import java.util.List;
import java.util.Map;

@Configuration
public class WebConfig {

    @ControllerAdvice
    static class MyControllerAdvice{
        @ExceptionHandler
        @ResponseBody
        public Map<String,String> handleEx(Exception exception){
            return Map.of("error",exception.getMessage());
        }
    }
    @Bean
    public ExceptionHandlerExceptionResolver resolver(){
        ExceptionHandlerExceptionResolver exceptionResolver=new ExceptionHandlerExceptionResolver();
        exceptionResolver.setMessageConverters(List.of(new MappingJackson2HttpMessageConverter()));
        return exceptionResolver;
    }

}
